package com.mymarkdown.app.fx.assemblys;

import com.mymarkdown.app.common.constant.Constant;
import com.mymarkdown.app.fx.enums.FxTreeItemTypeEnum;
import javafx.scene.Node;
import javafx.scene.control.TreeItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.FlowPane;
import javafx.scene.text.Text;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.io.File;
import java.util.Objects;
import java.util.UUID;

/**
 * @author ：zenglun
 * @date ：Created in 2021/12/22 2:29 PM
 * @description：
 * @modified By：
 * @version: $
 */
@Getter
@Setter
@Accessors(chain = true)
public class FxTreeItem<T> extends TreeItem<T> {
    /**
     * id
     */
    private String id;
    /**
     * 是否可以添加节点
     */
    private boolean isAddChildNode;
    /**
     * 是否可以编辑节点
     */
    private boolean isEdit;

    private boolean isOpen;

    private boolean isClear;

    private File sourceFile;

    private Text text;

    private FxTreeItemTypeEnum treeItemTypeEnum;


    /**
     *
     * @param value 要展示的节点名称
     * @param file 关联的文件
     * @param <T>
     * @return
     */
    public static <T>FxTreeItem<T> getFileTreeItem(T value, File file){
        return new FxTreeItem(value).setImage(Constant.AllLogsPath.FILE_LOG_PATH)
                .setEdit(true).setSourceFile(file).setAddChildNode(true)
                .setClear(true).setOpen(true)
                .setId(UUID.randomUUID().toString()).setTreeItemTypeEnum(FxTreeItemTypeEnum.FILE_TYPE);
    }

    public static <T>FxTreeItem<T> getDirTreeItem(T value, File file){
        return new FxTreeItem(value).setImage(Constant.AllLogsPath.DIR_LOG_PATH).setEdit(true)
                .setSourceFile(file)
                .setAddChildNode(false)
                .setClear(true).setOpen(true)
                .setId(UUID.randomUUID().toString()).setTreeItemTypeEnum(FxTreeItemTypeEnum.DIR_TYPE);
    }

    public static <T>FxTreeItem<T> getUpperLevelTreeItem(T value){
        return new FxTreeItem<>(value).setTreeItemTypeEnum(FxTreeItemTypeEnum.UPPER_LEVEL_TYPE)
                .setOpen(false).setClear(false).setAddChildNode(false)
                .setId(UUID.randomUUID().toString());
    }

    public static <T>FxTreeItem<T> getRootItem(T value){
        return new FxTreeItem<>(value).setTreeItemTypeEnum(FxTreeItemTypeEnum.ROOT)
                .setOpen(false).setClear(false).setAddChildNode(false)
                .setId(UUID.randomUUID().toString());
    }

    public static <T>FxTreeItem<T> getHistoricalOperationItem(T value){
        return new FxTreeItem<>(value).setTreeItemTypeEnum(FxTreeItemTypeEnum.HISTORICAL_OPERATION)
                .setOpen(false).setClear(false).setAddChildNode(false)
                .setId(UUID.randomUUID().toString());
    }

    public static <T>FxTreeItem<T> getCurrentOperationFileListItem(T value){
        return new FxTreeItem<>(value).setTreeItemTypeEnum(FxTreeItemTypeEnum.CURRENT_OPERATION_FILE_LIST)
                .setOpen(false).setClear(true).setAddChildNode(false)
                .setId(UUID.randomUUID().toString());
    }

    public FxTreeItem() {
        super();
    }

    private FxTreeItem setImage(String url) {
        ImageView imageView = new ImageView();
        imageView.setImage(new Image(url));
        imageView.setFitWidth(Constant.DEFAULT_LOG_WIDTH);
        imageView.setFitHeight(Constant.DEFAULT_LOG_HEIGHT);
        FlowPane flowPane = new FlowPane();
        this.text = new Text(Objects.toString(this.getValue()));
        flowPane.getChildren().addAll(imageView, text);
        this.setGraphic(flowPane);
        return this;
    }

    public Text getText() {
        return text;
    }

    public FxTreeItem(T value) {
        super(value);
    }

    public FxTreeItem(T value, Node graphic) {
        super(value, graphic);
    }
}
